{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 可以选择不同行的版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "58/58% |+++++++++++++++++++++++++| "
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
      "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
      "\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "PROBER = \"TCPIP0::192.168.0.2::800::SOCKET\"\n",
    "SMU = \"GPIB0::17::INSTR\"\n",
    "SWITCH = \"GPIB0::22::INSTR\"\n",
    "\n",
    "import pyvisa\n",
    "rm = pyvisa.ResourceManager()\n",
    "\n",
    "linkCheck = True\n",
    "try:\n",
    "    prober = rm.open_resource(PROBER)\n",
    "    prober.read_termination = '\\r\\n'\n",
    "    prober.query(\"*IDN?\")\n",
    "except Exception as e:\n",
    "    print(\"MPI Prober Station Link Error!\")\n",
    "    prober = None\n",
    "    linkCheck =  False\n",
    "try:\n",
    "    B1500a = rm.open_resource(SMU)\n",
    "    B1500a.query(\"*IDN?\")\n",
    "except Exception as e:\n",
    "    print(f\"B1500 SMU Link Error!\")\n",
    "    linkCheck =  False\n",
    "    B1500a = None\n",
    "try:\n",
    "    B2201 = rm.open_resource(SWITCH)\n",
    "    B2201.query(\"*IDN?\")\n",
    "except Exception as e:\n",
    "    print(\"B2201 Switching Matrix Link Error!\")\n",
    "    linkCheck =  False\n",
    "    B2201 = None\n",
    "\n",
    "def contact(prober=prober):\n",
    "    prober.write(\"move_chuck_contact\")\n",
    "def sep(prober=prober):\n",
    "    prober.write(\"move_chuck_separation\")\n",
    "def nextdie(prober=prober):\n",
    "    prober.write(\"map:step_next_die\")\n",
    "def subdie(prober,x,y,xoffset=0,yoffset=0):\n",
    "    prober.write(f'map:step_specify_die {x},{y},{xoffset},{yoffset}')\n",
    "\n",
    "def matrix(stat = 4,b2201=B2201):\n",
    "    import B2200\n",
    "    import time\n",
    "    #1,1234-1234\n",
    "    #2,1234-5678\n",
    "    #3,1234-9-12\n",
    "    #4,openall\n",
    "    B2200.Load_Path(b2201,stat)\n",
    "\n",
    "def closeIns():\n",
    "    if prober:\n",
    "        prober.close()\n",
    "    if B1500a:\n",
    "        B1500a.close()\n",
    "    if B2201:\n",
    "        B2201.close()\n",
    "    rm.close()  \n",
    "    \n",
    "from gtamp2 import *\n",
    "\n",
    "def trans(DeviceType = 'P'):\n",
    "    if DeviceType == 'P':\n",
    "        startV, endV, Vds = -2000, 500, -2000\n",
    "    elif DeviceType == 'N':\n",
    "        startV, endV, Vds = -500, 2000, 2000\n",
    "    \n",
    "    result = sendCmd(pointMode(0,Vds,points=5))\n",
    "    result = sendCmd(sweepMode(startV=startV,endV=endV,channel='01'))\n",
    "    dataTable = hex2table(result,columns = ['GateV','SourceI','ch3',\"ch4\"])\n",
    "    return dataTable\n",
    "\n",
    "#jupyter选择弹窗模式\n",
    "%matplotlib qt\n",
    "\n",
    "def autoTest():\n",
    "    if not linkCheck:\n",
    "        return \n",
    "    import pandas as pd \n",
    "    from datetime import datetime\n",
    "    tStamp = datetime.strftime(datetime.now(),\"%Y%m%d_%H%M%S\")\n",
    "    import os\n",
    "    os.mkdir(f'autoData/{tStamp}')\n",
    "\n",
    "    xlsFiles = {}\n",
    "    for position in [1,2,3]:\n",
    "        for dType in ['P']:\n",
    "            fn = f'{dType}MOS{str(position)}'\n",
    "            xlsFiles[fn] = pd.ExcelWriter(f'autoData/{tStamp}/{fn}.xlsx')\n",
    "\n",
    "\n",
    "\n",
    "    import matplotlib.pyplot as plt\n",
    "    import numpy as np\n",
    "    import time\n",
    "\n",
    "    plt.style.use('default')\n",
    "    plt.ion() #开启interactive mode 成功的关键函数\n",
    "    plt.cla()\n",
    "    plt.xlabel('GateV (V)')\n",
    "    plt.ylabel('SourceI (A)')\n",
    "    plt.yscale('log')\n",
    "    plt.title('StarAutoProbe')\n",
    "\n",
    "    import time\n",
    "    for i in range(58):\n",
    "        nextdie()\n",
    "        time.sleep(0.3)\n",
    "        contact()\n",
    "        time.sleep(0.3)\n",
    "        for position in [1,2,3]:\n",
    "            matrix(position)\n",
    "            time.sleep(0.2)\n",
    "            for dType in ['P']:\n",
    "                fn = f'{dType}MOS{str(position)}'\n",
    "                print('\\r'+str(i+1)+f'/58:{fn}',end='')\n",
    "                df = trans()\n",
    "                plt.title(\"StarAutoProbe: \"+str(i+1)+f'/58:{fn}')\n",
    "                plt.plot(df['GateV'],np.abs(df['SourceI']))\n",
    "                plt.pause(0.1)\n",
    "                df.to_excel(xlsFiles[fn],index=False,sheet_name=f\"Run{i}\")\n",
    "            matrix(4)\n",
    "        print('\\r'+str(i+1)+'/58',end='')\n",
    "        if i !=57:\n",
    "            sep()\n",
    "    plt.title('StarAutoProbe: Complete!')\n",
    "    plt.pause(0.1)\n",
    "\n",
    "    for position in [1,2,3]:\n",
    "        for dType in ['P']:\n",
    "            fn = f'{dType}MOS{str(position)}'\n",
    "            xlsFiles[fn].close()\n",
    "autoTest()\n",
    "closeIns()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
